﻿using System.Text;
using Core.SyntaxAnalysis;
using Core.SyntaxAnalysis.Classes;

namespace Core
{
    public class CodeGenerator
    {
        public string Generate(Program p)
        {
            string code = p.Generate();
            return code;
        }

        public static void AppendGoto(StringBuilder s, int id)
        {
            s.AppendLine("GOTO " + id);
        }

        public static void AppendGoto(StringBuilder s, Label l)
        {
            s.AppendLine("GOTO " + l.Number.StrValue);
        }

        public static void AppendLabel(StringBuilder s, int id)
        {
            s.AppendLine("LABEL " + id);
        }

        public static void AppendLabel(StringBuilder s, Label l)
        {
            s.AppendLine("LABEL " + l.Number.StrValue);
        }

        public static void AppendIfNotGoto(StringBuilder s, string ifnotId, int gotoId)
        {
            s.AppendLine("IFNOT " + ifnotId + " GOTO " + gotoId);
        }

        public static void AppendIncrement(StringBuilder s, string varibale)
        {
            s.AppendLine(varibale + "++");
        }

        public static void AppendFuncCall(StringBuilder s, FuncCall c)
        {
            foreach (Expr parameter in c.Parameters)
            {
                s.AppendLine("STACK_PUSH " + parameter.Generate(s));
            }
            s.AppendLine("CALL " + c.Lex.StrValue);
        }
    }
}